package com.llkj.qingnianguoxue.course;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.BookingCourseAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.BookAnnouncementBean;
import com.llkj.qingnianguoxue.bean.BookingCourseBean;
import com.llkj.qingnianguoxue.bean.RxCoursePayBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GridSpacingItemDecoration;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;
import com.llkj.qingnianguoxue.util.RxBus;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

import rx.functions.Action1;

public class BookingAnnouncementActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvCourseView,lvRecommendView;
    private BookingCourseAdapter mBookingAdapter,mRecomendAdapter;
    private List<BookingCourseBean> mBookData,mRecommendData;
    private int totalSize,mPage;
    private ImageView mIvBg,mIvHead;
    private TextView mUserName;
    private String mMasterId;//课程大师id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_booking_announcement);
        setTitle("预约公告", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);
        Intent intent = getIntent();
        mMasterId = intent.getStringExtra("id");

        initView();
        initListener();
//        loadWaitDialog();
        onRefresh();
    }

    private void initView(){
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvCourseView = (RecyclerView) findViewById(R.id.recyclerView);
        lvCourseView.setLayoutManager(new GridLayoutManager(this, 3));
        lvCourseView.addItemDecoration(new GridSpacingItemDecoration(3, 10, false));
        lvCourseView.setPadding(DisplayUtil.dip2px(this,10),0, DisplayUtil.dip2px(this,10),0);

        mBookData = new ArrayList<>();
        mBookingAdapter = new BookingCourseAdapter(R.layout.item_booking_course,mBookData);
        lvCourseView.setAdapter(mBookingAdapter);

        mIvBg = (ImageView) findViewById(R.id.iv_bg);
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mIvBg.getLayoutParams();
        params.height = DisplayUtil.getDensityWdith(this)*248/640;
        mIvHead = (ImageView) findViewById(R.id.iv_head);
        mUserName = (TextView) findViewById(R.id.tv_name);

        lvRecommendView = (RecyclerView) findViewById(R.id.lvFooter);
        lvRecommendView.setPadding(DisplayUtil.dip2px(this,10),0, DisplayUtil.dip2px(this,10),0);
        lvRecommendView.setLayoutManager(new GridLayoutManager(this, 3));
        lvRecommendView.addItemDecoration(new GridSpacingItemDecoration(3, 10, false));
        mRecommendData = new ArrayList<>();
        mRecomendAdapter = new BookingCourseAdapter(R.layout.item_booking_course,mRecommendData);
        lvRecommendView.setAdapter(mRecomendAdapter);

        refreshLayout.setOnRefreshListener(this);
//        mRecomendAdapter.setOnLoadMoreListener(this);
    }

    private void initListener(){
        lvCourseView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                BookingCourseBean item = mBookData.get(i);
                Bundle bundle = new Bundle();
                bundle.putString("title",item.getName());
                bundle.putString("id",item.getId());
                startActivity(CourseDetailActivity.class,bundle);
            }
        });

        lvRecommendView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                BookingCourseBean item = mRecommendData.get(i);
                Bundle bundle = new Bundle();
                bundle.putString("title",item.getName());
                bundle.putString("id",item.getId());
                startActivity(CourseDetailActivity.class,bundle);
            }
        });

        RxBus.getInstance().toObservable(RxCoursePayBean.class).subscribe(new Action1<RxCoursePayBean>() {
            @Override
            public void call(RxCoursePayBean bean) {
                if (bean.isPaySuccess){
                   onRefresh();
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        HttpMethod.indexOrder(this,this,mMasterId);
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, mBookingAdapter, null, lvCourseView, totalSize);
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        refreshLayout.setRefreshing(false);
        if (HttpMethod.HTTP_INDEX_ORDER == method) {//预约公告列表
            BookAnnouncementBean bean = GsonUtil.GsonToBean(data,BookAnnouncementBean.class);
            if(bean!=null){
                BookAnnouncementBean.MasterBean masterBean = bean.getMaster();
                if(masterBean!=null){
                    ImageLoadingUtil.loadingCircleBorderImg(mIvHead, masterBean.getImg(), R.color.transparent,0);
                    ImageLoadingUtil.loadBlurTransImg(mIvBg, masterBean.getImg());
                    mUserName.setText(masterBean.getName());
                    mBookData.clear();
                    mBookData.addAll(masterBean.getCurriculum());
                    mBookingAdapter.notifyDataSetChanged();
                }

//                totalSize = Integer.parseInt(hotRankingBean.getNum());
//                if (mPage == 1) {
//                    mRecommendData.clear();
//                    LoadDataHelper.refreshComplete(videoAdapter, mVideosData, refreshLayout, lvActions);
//                } else {
//                    mRecomendAdapter.setEnableLoadMore(true);
//                    mRecomendAdapter.loadMoreComplete();
//                }
                mRecommendData.clear();
                mRecommendData.addAll(bean.getCurriculum());
                mRecomendAdapter.notifyDataSetChanged();
            }
        }
    }
}
